package com.yay.rental.io;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;


/**
 * Get Started!
 * @author team@getstarted.com.ua
 */
public class DataHandler {
	private static final String TAG = "DataHandler";
	private SQLiteDatabase db;
	private Context context;
	private final static String SEP = ";";
    
	
	public DataHandler(Context context, SQLiteDatabase db) {
		 this.context = context;
		 this.db = db;
	}
	
	
    public void insertCategories() {
    	BufferedReader reader = null;
        InputStreamReader in = null;
        SQLiteStatement stmt = null;
        try{
        	in = new InputStreamReader(context.getAssets().open("categories.csv"));
            reader = new BufferedReader(in, 1024);
        	db.beginTransaction();
    		String[] data = null;
    		String text = null;
    		stmt = db.compileStatement("insert into categories (_id, idp, name) values (?,?,?)");
            while(((text = reader.readLine()) != null)) {
                data = text.split(SEP);
                stmt.bindLong(1, Integer.valueOf(data[0]));
                stmt.bindLong(2, Integer.valueOf(data[1]));
                stmt.bindString(3, data[2]);
                stmt.execute();
            }
            db.setTransactionSuccessful();
        } catch(Exception e) {
        	Log.e(TAG, e.toString());
        } finally {
        	try {
        		db.endTransaction();
        		stmt.close();
        		if(reader != null) {
                    reader.close();
                }
                if(in != null) {
                	in.close();
                }
            } catch(Exception e) {
            	Log.e(TAG, e.toString());
            }
        }
    }

    public void insertRentals() {
    	BufferedReader reader = null;
        InputStreamReader in = null;
        SQLiteStatement stmt = null;
        try{
        	in = new InputStreamReader(context.getAssets().open("rentals.csv"));
            reader = new BufferedReader(in, 1024);
        	db.beginTransaction();
    		String[] data = null;
    		String text = null;
    		stmt = db.compileStatement("insert into rentals (_id, idp, name, price, accommodations, description) values (?,?,?,?,?,?)");
            while(((text = reader.readLine()) != null)) {
                data = text.split(SEP);
                stmt.bindLong(1, Integer.valueOf(data[0]));
                stmt.bindLong(2, Integer.valueOf(data[1]));
                stmt.bindString(3, data[2]);
                stmt.bindString(4, data[3]);
                stmt.bindString(5, data[4]);
                stmt.bindString(6, data[5]);
                stmt.execute();
            }
            db.setTransactionSuccessful();
        } catch(Exception e) {
        	Log.e(TAG, e.toString());
        } finally {
        	try {
        		db.endTransaction();
        		stmt.close();
        		if(reader != null) {
                    reader.close();
                }
                if(in != null) {
                	in.close();
                }
            } catch(Exception e) {
            	Log.e(TAG, e.toString());
            }
        }
    }

}
